package com.zmx.product.service;

import com.nepxion.aquarius.cache.annotation.Cacheable;
import com.zmx.common.common.contents.cache.Expire;
import com.zmx.common.common.service.BaseService;
import com.zmx.common.common.contents.cache.ProductCacheConstents;
import com.zmx.product.dto.ProductCategoryDto;
import com.zmx.product.dto.result.IndexCategoryDTO;
import com.zmx.product.entity.ProductCategory;
import com.zmx.product.repository.ProductCategoryRepository;
import com.zmx.product.service.mapper.ProductCategoryMapper;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhaomxr
 */
@Service
public class ProductCategoryService extends BaseService<ProductCategory, ProductCategoryDto, ProductCategoryRepository, ProductCategoryMapper> {

    /**
     * 获取所有一级分类
     * @return
     */
    @Cacheable(name = ProductCacheConstents.PRODUCT_CATEGORY, key = "'getCategory'", expire = Expire.FOREVER)
    public List<ProductCategoryDto> getCategory() {
        Map<String, Object> params = new HashMap<String, Object>(1);
        params.put("ranking_sort", Sort.Direction.DESC);

        List<ProductCategoryDto> categoryDtos = this.getAll(params);
        return categoryDtos;
    }

    /**
     * 只取排名前四个分类
     * @return
     */
    @Cacheable(name = ProductCacheConstents.PRODUCT_CATEGORY, key = "'getCategoryOnly4'", expire = Expire.FOREVER)
    public List<IndexCategoryDTO> getCategoryOnly4() {
        List<ProductCategory> categoryList = repository.getCategoryOnly4();

        List<IndexCategoryDTO> resultList = new ArrayList<IndexCategoryDTO>();
        for (ProductCategory category : categoryList) {
            IndexCategoryDTO indexCategoryDTO = new IndexCategoryDTO();
            indexCategoryDTO.setId(category.getId());
            indexCategoryDTO.setName(category.getName());
            //暂时将分类的icon放在remark中
            indexCategoryDTO.setIcon(category.getRemark());
            indexCategoryDTO.setDisabled(false);
            resultList.add(indexCategoryDTO);
        }
        //在首页分类展示的list最后拼接一条 更多选项
        IndexCategoryDTO indexCategoryDTO = new IndexCategoryDTO();
        indexCategoryDTO.setId(null);
        indexCategoryDTO.setName("更多");
        indexCategoryDTO.setIcon("http://49.233.105.77:8088/image/icon/more.png");
        indexCategoryDTO.setDisabled(false);
        resultList.add(indexCategoryDTO);
        return resultList;
    }
}
